package xps.and.uudaijia.userclient.view.activity;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.data.baen.CodeBean;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.CommonUtils;
import xps.and.uudaijia.userclient.util.DownTimer;
import xps.and.uudaijia.userclient.view.base.BaseActivity;

/**
 * Created by Administrator on 2017/7/11.
 */

public class RegisterActivity extends BaseActivity {
    public static int Interval = 1000;
    @BindView(R.id.Iphone_EditText)
    EditText IphoneEditText;
    @BindView(R.id.Verification_EditText)
    EditText VerificationEditText;
    @BindView(R.id.Verification_TextView)
    TextView VerificationTextView;
    @BindView(R.id.Password_EditText)
    EditText PasswordEditText;
    @BindView(R.id.OKPassword_EditText)
    EditText OKPasswordEditText;
    @BindView(R.id.Male_CheckBox)
    CheckBox MaleCheckBox;
    @BindView(R.id.Girl_CheckBox)
    CheckBox GirlCheckBox;
    @BindView(R.id.Submit_Button)
    Button SubmitButton;
    DownTimer timer;
    String IphoneString = "";
    String VerificationString = "";
    String PasswordString = "";
    String OKPasswordString = "";
    String CheckBoxString = "1";
    @BindView(R.id.iv_back)
    ImageView iv_back;

    @Override
    protected int getLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
    }

    @OnClick({R.id.Verification_TextView, R.id.Submit_Button, R.id.Male_CheckBox, R.id.Girl_CheckBox, R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.Verification_TextView:
                if (!CommonUtils.isMobileNumber(IphoneEditText.getText
                        ().toString().trim())) {
                    JUtils.Toast("手机号码格式不合法");
                    return;
                }
                IphoneString = IphoneEditText.getText().toString();
                Verification();
                break;
            case R.id.Submit_Button:

                IphoneString = IphoneEditText.getText().toString();
                VerificationString = VerificationEditText.getText().toString();
                OKPasswordString = OKPasswordEditText.getText().toString();
                PasswordString = PasswordEditText.getText().toString();
                if (OKPasswordString.equals(PasswordString)) {
                    submit();
                } else {
                    JUtils.Toast("两次密码不相同 ，请重新输入");
                }

                break;
            case R.id.Male_CheckBox:
                if (MaleCheckBox.isChecked()) {
                    GirlCheckBox.setClickable(false);
                } else {
                    GirlCheckBox.setClickable(true);
                }
                CheckBoxString = "1";
                break;
            case R.id.Girl_CheckBox:
                if (GirlCheckBox.isChecked()) {
                    MaleCheckBox.setClickable(false);
                } else {
                    MaleCheckBox.setClickable(true);
                }
                CheckBoxString = "2";
                break;
            case R.id.iv_back:
                finish();
                break;
        }
    }

    void Verification() {
        UserNetWorks.getVerification(IphoneString, new Subscriber<CodeBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(CodeBean codeBean) {
                if (codeBean.getReturn_code().equals("SUCCESS")) {
                    JUtils.Toast("发送成功");
                    VerificationTextView.setEnabled(false);
                    timer = new DownTimer();
                    timer.setTotalTime(60 * Interval);
                    timer.setIntervalTime(Interval);
                    timer.setTimerLiener(new DownTimer.TimeListener() {
                        @Override
                        public void onFinish() {
                            VerificationTextView.setEnabled(true);
                            VerificationTextView.setText("获取验证码");
                        }

                        @Override
                        public void onInterval(long remainTime) {
                            VerificationTextView.setText(remainTime / 1000
                                    + "秒后" + "再试");
                        }
                    });
                    timer.start();
                } else if (codeBean.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(codeBean.getReturn_msg());
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer = null;
    }

    void submit() {
        UserNetWorks.getSubmit(VerificationString, IphoneString, OKPasswordString, CheckBoxString, new Subscriber<CodeBean>() {

            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                e.printStackTrace();
            }

            @Override
            public void onNext(CodeBean codeBean) {
                Log.e("CodeBean", codeBean.toString());
                if (codeBean.getReturn_code().equals("SUCCESS")) {
                    JUtils.Toast("注册成功");
                    finish();
                } else if (codeBean.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(codeBean.getReturn_msg());
                }
            }
        });
    }
}
